<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="登记编号" prop="serial">
        <el-input v-model="queryParams.serial" placeholder="请输入登记编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="快递人" prop="recipient">
        <el-input v-model="queryParams.recipient" placeholder="请输入快递人" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="村小组" prop="villageGroup">
        <el-select v-model="queryParams.villageGroup" placeholder="请选择村小组" clearable>
          <el-option v-for="dict in dict.type.village_group" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="联系方式" prop="contactPhone">
        <el-input v-model="queryParams.contactPhone" placeholder="请输入联系方式" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option v-for="dict in dict.type.express_status" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['system:express:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate" v-hasPermi="['system:express:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-check" size="mini" :disabled="single" @click="handleDeliver" v-hasPermi="['system:express:deliver']">送达</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['system:express:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" v-hasPermi="['system:express:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="expressList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="登记编号" align="center" prop="serial" width="160" />
      <el-table-column label="快递人" align="center" prop="recipient" width="120" />
      <el-table-column label="联系方式" align="center" prop="contactPhone" width="140" />
      <el-table-column label="取件码" align="center" prop="pickupCode" width="120" />
      <el-table-column label="村小组" align="center" prop="villageGroup" width="120">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.village_group" :value="scope.row.villageGroup" />
        </template>
      </el-table-column>
      <el-table-column label="登记人" align="center" prop="userName" width="120" />
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.express_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="登记时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="送达时间" align="center" prop="deliverTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.deliverTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:express:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-check" @click="handleDeliver(scope.row)" v-hasPermi="['system:express:deliver']">送达</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['system:express:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="快递人" prop="recipient">
              <el-input v-model="form.recipient" placeholder="请输入快递人" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="取件码" prop="pickupCode">
              <el-input v-model="form.pickupCode" placeholder="请输入取件码" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="联系方式" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系方式" />
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="村小组" prop="villageGroup">
              <el-select v-model="form.villageGroup" placeholder="请选择村小组">
                <el-option v-for="dict in dict.type.village_group" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="status">
              <el-select v-model="form.status" placeholder="请选择状态">
                <el-option v-for="dict in dict.type.express_status" :key="dict.value" :label="dict.label" :value="dict.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="登记人" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入登记人" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listExpress, getExpress, delExpress, addExpress, updateExpress, deliverExpress } from '@/api/system/express'

export default {
  name: 'Express',
  dicts: ['express_status', 'village_group'],
  data() {
    return {
      loading: false,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      expressList: [],
      title: '',
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        serial: null,
        recipient: null,
        contactPhone: null,
        villageGroup: null,
        status: null
      },
      form: {},
      rules: {
        recipient: [
          { required: true, message: '快递人不能为空', trigger: 'blur' }
        ],
        contactPhone: [
          { required: true, message: '联系方式不能为空', trigger: 'blur' }
        ],
        pickupCode: [
          { required: true, message: '取件码不能为空', trigger: 'blur' }
        ],
        villageGroup: [
          { required: true, message: '村小组不能为空', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listExpress(this.queryParams).then(response => {
        this.expressList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '新增快递登记'
    },
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getExpress(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = '修改快递登记'
      })
    },
    handleDeliver(row) {
      const id = row && row.id ? row.id : (this.ids.length === 1 ? this.ids[0] : null)
      if (!id) {
        this.$modal.msgWarning('请选择需要送达的记录')
        return
      }
      this.$modal.confirm('是否确认将该快递标记为已送达？').then(() => {
        return deliverExpress(id)
      }).then(() => {
        this.$modal.msgSuccess('送达成功')
        this.getList()
      }).catch(() => {})
    },
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除快递登记编号为"' + ids + '"的数据项？').then(function() {
        return delExpress(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {})
    },
    handleExport() {
      this.download('/api/express/export', {
        ...this.queryParams
      }, `express_${new Date().getTime()}.xlsx`)
    },
    cancel() {
      this.open = false
      this.reset()
    },
    reset() {
      this.form = {
        id: null,
        serial: null,
        userName: null,
        villageGroup: null,
        recipient: null,
        contactPhone: null,
        pickupCode: null,
        remark: null,
        status: 'pending'
      }
      this.resetForm('form')
    },
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateExpress(this.form).then(() => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addExpress(this.form).then(() => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    }
  }
}
</script>

